const downloadMenthod = {
    exportByXML(url,token,fileName,terminal){
        var baseURL = "http://report.wsn2019.club/download/"
        function createObjectURL(object) { 
          return (window.URL) ? window.URL.createObjectURL(object) : window.webkitURL.createObjectURL(object); 
        }
        var xhr = new XMLHttpRequest();
        // 创建一个get请求，采用异步
        xhr.open('get', baseURL + url,true) 
        xhr.setRequestHeader("Authorization", token)
        //设置响应返回的数据格式
        xhr.responseType = 'blob'
        // 相关事件回调处理函数
        const that = this
        xhr.onload = function () {
          if (this.status == 200) {
            //   blob的type属性，除了chrom和samsung都不支持
            const blob = that.getBlob(this.response)
            console.log("blob:" + blob)
            // const blob = new Blob([this.response],{type:"application/vnd.ms-excel"}) 
            if('msSaveOrOpenBlob' in navigator){
                alert("msSaveOrOpenBlob")
                window.navigator.msSaveOrOpenBlob(blob, filename + '.xls');
                return;
            }
            var a = document.createElement('a')
            var blobUrl = createObjectURL(blob)
            alert("blobUrl:" + blobUrl)
        //   if(terminal === 'ios'){
        //     a.href = baseURL + url
        //   }else{
        //     a.href = blobUrl // 通过blob，手机端不行
        //   }
            a.href = blobUrl
        //   a.download = fileName
            a.setAttribute('download',fileName)
            a.click()
            window.URL.revokeObjectURL(blobUrl)
          }else{
            alert("接口报错：" + this.status)
          }
        }
        xhr.send()
    },
// 部分浏览器(webview)不支持HTML5新增的Blob, 
// 会抛出异常TypeError Illegal Constructor, 
// 这种情况可以使用BlobBuilder做兼容处理
    getBlob(svg) {
        window.BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder
        let blob = null;
        try {
            blob = new Blob([svg], { type: 'application/vnd.ms-excel' });
        } catch (err) {
            if (window.BlobBuilder) {
                const bb = new window.BlobBuilder();
                bb.append([svg]);
                blob = bb.getBlob('application/vnd.ms-excel');
            }
        }
        return blob
    }
}
export default downloadMenthod
